package com.feifan.monthhelper.ui.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.feifan.monthhelper.R;
import com.feifan.monthhelper.bean.db.DayRecordBean;
import com.feifan.monthhelper.constant.Constant;
import com.feifan.monthhelper.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 爱爱记录适配器
 */
public class LoveRecordAdapter extends RecyclerView.Adapter<LoveRecordAdapter.ProtectHolder> {
    private static final int ITEM_HEADER = 2;
    private static final int ITEM_NOTMAL = 0;

    private List<DayRecordBean> protects = new ArrayList<>();

    @NonNull
    @Override
    public ProtectHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ProtectHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_love_record, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ProtectHolder protectHolder, int i) {
        DayRecordBean dayRecordBean = protects.get(i);
        protectHolder.tvStartDate.setText(DateUtils.init().dateToString(dayRecordBean.createDate, DateUtils.DateStyle.YYYY_MM_DD));
        if (dayRecordBean.state == Constant.MenstrualState.OVULATORY_DAY) {
            protectHolder.tvPeriod.setText(R.string.ovulation);
        } else if (dayRecordBean.state == Constant.MenstrualState.MENSTRUAL_DAY) {
            protectHolder.tvPeriod.setText(R.string.menstruation);
        } else if (dayRecordBean.state == Constant.MenstrualState.SAFE_DAY) {
            protectHolder.tvPeriod.setText(R.string.safe_days);
        } else {
            protectHolder.tvPeriod.setText(R.string.other_period);
        }
        if(dayRecordBean.protects != null && dayRecordBean.protects.size() > 0) {
            protectHolder.tvLoveProtect.setText(dayRecordBean.protects.get(0));
        } else {
            protectHolder.tvLoveProtect.setText("");
        }
    }


    @Override
    public int getItemCount() {
        return protects == null ? 0 : protects.size();
    }

    public void clear() {
        protects.clear();
        notifyDataSetChanged();
    }

    public void add(List<DayRecordBean> data) {
        protects.addAll(data);
        notifyDataSetChanged();
    }

    public void replace(List<DayRecordBean> data) {
        protects.clear();
        protects.addAll(data);
        notifyDataSetChanged();
    }

    class ProtectHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_start_date)
        TextView tvStartDate;
        @BindView(R.id.tv_period)
        TextView tvLoveProtect;
        @BindView(R.id.tv_love_protect)
        TextView tvPeriod;

        public ProtectHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
